package com.woniuxy.enjoylife.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;

import com.github.pagehelper.PageHelper;
import com.woniuxy.enjoylife.entity.Server_order;

import com.woniuxy.enjoylife.entity.dto.ReturnServerOrderDto;
import com.woniuxy.enjoylife.entity.dto.SelectServerOrderDto;
import com.woniuxy.enjoylife.entity.dto.ServerAndOrderDto;
import com.woniuxy.enjoylife.service.Server_orderService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import com.woniuxy.enjoylife.utils.ResponseResult;
import com.woniuxy.enjoylife.utils.SetUid;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/serverOrder")
public class Server_orderController {
    @Resource
    private Server_orderService serverOrderService;
    @Resource
    private HttpServletRequest request;


    //新增服务订单
    @PostMapping("/insertServerOrder")
    public boolean insertServerOrder(@RequestBody Server_order order){
        int userid=(int) request.getSession().getAttribute("id");
//        System.out.println(order);
        order.setCreatetime(LocalDateTime.now());
        return serverOrderService.insertServerOrder(order,userid);
//        return  false;
    }

    //旧的
    @RequestMapping("/selectServerOrder")
    public PageResult<Server_order> selectServerOrder(SelectServerOrderDto dto){
        dto.setUid((int) request.getSession().getAttribute("id"));
        return serverOrderService.selectServerOrder(dto);
    }

    //新的
    @RequestMapping("/selectServerOrderRebuild")
    public PageResult<ReturnServerOrderDto> selectServerOrderRebuild(SelectServerOrderDto dto){
        dto.setUid((int) request.getSession().getAttribute("id"));
        return serverOrderService.selectServerOrderDto(dto);
    }

    @RequestMapping("/updateServerOrder")
    public boolean updateServerOrder(@RequestBody Server_order serverOrder){
        System.out.println("参数======="+serverOrder.toString());
        serverOrder.setUid((int) request.getSession().getAttribute("id"));
        return serverOrderService.updateServerOrder(serverOrder);
    }

    @RequestMapping("/deleteServerOrder")
    public boolean deleteServerOrder(Server_order serverOrder){
        return serverOrderService.deleteServerOrder(serverOrder);
    }

//    商家服务订单===============================================================================================
//封装分页信息
public PageResult getResult(int currentPage, int size, Page pageHelper, List T) {
    //获取分页信息
    PageResult pageResult = new PageResult<>();
    //当前页
    pageResult.setCurrentPage(currentPage);
    //每一页大小
    pageResult.setPageSize(size);
    //总条数
    pageResult.setTotal(pageHelper.getTotal());
    //总页数
    pageResult.setTotalPage(pageHelper.getPages());
    //商品数据
    pageResult.setData(T);
    return pageResult;
}



    /**
     * 分页查询订单
     */
    @GetMapping("findByPage/{size}/{currentPage}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("size") Integer size,@PathVariable("currentPage") Integer currentPage){
        ResponseResult responseResult = new ResponseResult<>();
        int uid =(int) request.getSession().getAttribute("id");

        Page pageHelper = PageHelper.startPage(currentPage, size);
        List serverAndOrder = serverOrderService.findServerAndOrder(uid);
        System.out.println(serverAndOrder+"huoqufenye");

        List result = pageHelper.getResult();
//        System.out.println(result);
        PageResult pageResult = getResult(currentPage, size, pageHelper, serverAndOrder);

             responseResult.setData(pageResult);

        return responseResult;
    }



    /**
     * 取消订单
     */
    @DeleteMapping("deleteOne/{id}")
    @ResponseBody
    public ResponseResult deleteServerOrder(@PathVariable Integer id) {
        ResponseResult responseResult = new ResponseResult<>();
//        boolean result = serverOrderService.removeById(id);

        boolean result = serverOrderService.updateById(new Server_order().setId(id).setStatus(1));
        if (result) {
            responseResult.success();

        } else {
            responseResult.error();
        }

        return responseResult;
    }

    /**
     * 修改订单
     * @param
     * @return
     */
    @RequestMapping("updateById")
    @GetMapping
    public ResponseResult updateServerOrderById(Server_order serverOrder) {
        System.out.println("传送数据"+serverOrder);
        ResponseResult<Object> responseResult = new ResponseResult<>();

        boolean result = serverOrderService.saveOrUpdate(serverOrder);
        if (result) {
            responseResult.success();

        } else {
            responseResult.error();
        }
        return responseResult;
    }

        //查询ServerAndOrderDto
    @PostMapping("serverAndOrderDto/{serial}")
    @ResponseBody
    public ServerAndOrderDto findServerAndOrderDto(@PathVariable("serial") String serial){
        ResponseResult responseResult = new ResponseResult();

        ServerAndOrderDto serverAndOrderDto = serverOrderService.serverAndOrderDtoService(serial);
        System.out.println("dto类"+serverAndOrderDto);
//          responseResult.success();
          responseResult.setData(serverAndOrderDto);


        return serverAndOrderDto;


    }


    //********************************用户服务订单管理****************

    /**
     * 用户取消订单
     * @param id  订单id
     * @return
     * aythor:sj
     */
    @RequestMapping("/cancleOrder")
    @ResponseBody
    public ResponseResult cancleOrder(String id){
        System.out.println("用户服务订单id="+id);
        ResponseResult responseResult = new ResponseResult();
        if(serverOrderService.cancleOrder(Integer.parseInt(id))){
            responseResult.success();
        }else{
            responseResult.error();
        }
            return responseResult;

    }

    /**
     * 用户删除订单
     * @param id
     * @return
     */
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public ResponseResult deleteOrder(String id){
        ResponseResult responseResult = new ResponseResult();
        if(serverOrderService.deleteOrder(Integer.parseInt(id))){
            responseResult.success();
        }else{
            responseResult.error();
        }
        return responseResult;
    }

}

